package com.zhangyu.shadow.repair.delegate.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;

import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.base.RootDelegate;
import com.zhangyu.shadow.repair.config.ModuleConfig;
import com.zhangyu.shadow.repair.manager.AccountManager;

import butterknife.BindView;
import butterknife.OnClick;
import st.lib.base.delegate.mvp.MvpDelegate;
import st.lib.base.ui.progress.sweetalert.DialogLoader;
import st.lib.base.ui.search.SearchClearEditText;
import st.lib.base.util.utilcode.util.SnackbarUtils;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/5
 */
public class LoginDelegate extends MvpDelegate<LoginPresenter> implements LoginContract.View {

    public static LoginDelegate newInstance() {
        return new LoginDelegate();
    }

    @BindView(R2.id.et_account)
    SearchClearEditText mEtAccount;
    @BindView(R2.id.et_password)
    SearchClearEditText mEtPassword;
    @BindView(R2.id.fl_first_container)
    ConstraintLayout mConstraintLayout;
    private DialogLoader mDialogLoader;

    @Override
    public Object setLayout() {
        return R.layout.delegate_login;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mDialogLoader = new DialogLoader(getContext());
        mEtAccount.setHint("请输入工号");
        mEtAccount.setText(AccountManager.getInstance().getWorkCode());
    }

    @OnClick(R2.id.btn_login)
    public void clickMe(View view) {
        String account = mEtAccount.getText().toString();
        String password = mEtPassword.getText().toString();
        if (account.isEmpty() || password.isEmpty()) {
            SnackbarUtils.with(mConstraintLayout)
                    .setMessage("请输入正确的账号和密码")
                    .showWarning();
        } else {
            mPresenter.login(account, password, ModuleConfig.APP_ID);
        }
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void loginSuccess() {
        startWithPop(RootDelegate.newInstance());
    }

    @Override
    public void loginFailed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @Override
    public void showLoading(String str) {
        mDialogLoader.showProgressDialog(true, str,
                getString(R.string.loading_str_waiting));
    }

    @Override
    public void hideLoading() {
        mDialogLoader.clearDialog();
    }
}
